//
//  HSYMeViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/1.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYMineViewController.h"
#import "HSYMeHeaderView.h"
#import "HSYMineCell.h"
#import "BQActivityView.h"
#import "HSYSetWarningToneVCViewController.h"
#import "HSYShopManageViewController.h"
#import "HSYClerkManageViewController.h"

#define kHeaderViewHeight 233.0

@interface HSYMineViewController ()<UIAlertViewDelegate>

@property (nonatomic, strong) HSYMeHeaderView *tableHeader;
@property(nonatomic,strong)NSArray *dataList;


@end

@implementation HSYMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];

    //设置表头视图
    HSYMeHeaderView *header = [[[NSBundle mainBundle] loadNibNamed:@"HSYMeHeaderView" owner:nil options:nil] firstObject];
    header.frame = CGRectMake(0, 0, ScreenWidth(), kHeaderViewHeight);
    UIView *vi = [[UIView alloc] initWithFrame:header.bounds];
    [vi addSubview:header];
    
    self.tableHeader = header;
    
    self.tableView.tableHeaderView = vi;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.dataList = @[
                      @[@{@"icon":@"store",@"title":@"店铺管理"},@{@"icon":@"clerk",@"title":@"店员管理"}],
                      @[@{@"icon":@"voice",@"title":@"收款提示音"}],
                      @[@{@"icon":@"exit",@"title":@"退出登录"}]
                      ];
    
    
    if ([HSYUserCenter instatce].appAuUser.role != 1) {
        self.dataList =@[
                         @[@{@"icon":@"voice",@"title":@"收款提示音"}],
                         @[@{@"icon":@"exit",@"title":@"退出登录"}]
                         ];
        
    }
    
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)hsy_allowPullUp{
    return NO;
}

- (BOOL)hsy_allowPullDown{
    return NO;
}

- (UIEdgeInsets)hsy_tableViewEdgeInsets{
    return UIEdgeInsetsMake(0, 0, 49, 0);
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    [self updateHeaderView];
}

//刷新头部视图数据
-(void)updateHeaderView{
    self.tableHeader.phoneNumberLabel.text = [HSYUserCenter instatce].appAuUser.cellphone;
    [self.tableHeader.nickNameLabel setTitle:[HSYUserCenter instatce].appAuUser.realname forState:UIControlStateNormal];
}

#pragma mark - cell 点击事件 -

- (void)didLogout{
    [self showMessage:@"退出成功"];
    [APPCenter defaultCenter].password = nil; //移除钥匙串保存的用户名和密码
    [APPCenter defaultCenter].username = nil;
    
    [self updateHeaderView];
    self.tabBarController.selectedIndex = 0;

}


//退出登录
- (void)logout{
    [BQActivityView showActiviTy];
    
    [[APPCenter defaultCenter] cancelLoginIfNeed];
    __weak typeof(self)weakSelf = self;
    
    [[HSYUserCenter instatce] logoutWithCompletionBlock:^(BOOL result, NSString *errorStr) {
        [BQActivityView hideActiviTy];
        if (result) {
            [weakSelf didLogout];
        }else{
            [weakSelf showMessage:errorStr];
        }
    }];
}

- (BOOL)isUserInfoInReview{
    if ([[HSYUserCenter instatce].appBizShop.status isEqualToString:@"3"]) {
        alert_alam_tag(@"提示", @"您的审核信息被拒，是否开始审核", 100, @"取消", @"确定");
        return YES;
    }
    
    if ([[HSYUserCenter instatce].appBizShop.status isEqualToString:@"2"]) {
        alert_alam_cancel(@"提示", @"您的审核信息正在审核中", @"确定");
        return YES;
    }
    
    if ([[HSYUserCenter instatce].appBizShop.status isEqualToString:@"4"]) {
        alert_alam_tag(@"提示", @"您的审核信息审核信息不全是否现在去完善", 102, @"取消", @"确定");
        return YES;
    }
    
    return NO;
}

//店铺管理
- (void)pushToShopManage{
    HSYShopManageViewController * vc = [[HSYShopManageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//收款提示音
- (void)PushToSetPaymentWarningTone{
    HSYSetWarningToneVCViewController *vc = [[HSYSetWarningToneVCViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

//店员管理
- (void)pushToClerkManage{
    HSYClerkManageViewController * vc = [[HSYClerkManageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark  UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataList.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *arry = self.dataList[section];
    
    return arry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.0;
    }else{
        return 10.0;
    }
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    CGFloat hight = 0.0;
    
    if ((section =!0)) {
        hight = 10.0;
    }
    
    UIView *vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth(), hight)];
    vi.backgroundColor = [UIColor clearColor];
    
    return vi;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *model = self.dataList[indexPath.section][indexPath.row];
    
    HSYMineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeTableCell"];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HSYMineCell" owner:nil options:nil] firstObject];
    }
    
    
    if (indexPath.row == [self.dataList[indexPath.section] count] -1 || [self.dataList[indexPath.section] count] == 1) {
        cell.separatorViewLeftConstraint.constant = 0;
    }else{
        cell.separatorViewLeftConstraint.constant = 15;
    }
    
    cell.iconImageView.image = [UIImage imageNamed:model[@"icon"]];
    cell.titleLabel.text = model[@"title"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([HSYUserCenter instatce].appAuUser.role != 1){
        if (indexPath.section == 1 && indexPath.row == 0) {
            [self logout];
            return ;
        }
    }else{
        if (indexPath.section == 2 && indexPath.row == 0) {
            [self logout];
            return ;
        }
    }
    
    if ([self isUserInfoInReview]) return; //用户信息是否通过审核
    
    if ([HSYUserCenter instatce].appAuUser.role == 1){
        
        if (indexPath.section == 0 && indexPath.row == 0) {
            [self pushToShopManage]; //店铺管理
        }
        
        if (indexPath.section == 1 && indexPath.row == 0) {
            [self PushToSetPaymentWarningTone]; //收款提示音
        }
        
        if (indexPath.section == 0 && indexPath.row == 1) {
            [self pushToClerkManage]; //店员管理
        }
        
    }else{
        if (indexPath.section == 0 && indexPath.row == 0){
            [self PushToSetPaymentWarningTone]; //收款提示音
        }
    }
}


#pragma mark - UIAlertViewDelegate -
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == 100 || alertView.tag == 102) {
        if (buttonIndex != alertView.cancelButtonIndex) {
            [HSYGoToAuditUtil goAuditFromViewController:self.tabBarController push:NO]; //去完善信息
        }
    }
}



#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint contOffSet = scrollView.contentOffset;
    CGFloat height = -contOffSet.y;
    
    if (height<0) {
        self.tableHeader .frame = CGRectMake(0, 0, ScreenWidth(), 233.0 );
    }else{
        CGRect rt = self.tableHeader.frame;
        rt.origin.y = -height;
        rt.size.height = 233.0  + height;
        self.tableHeader.frame = rt;
    }
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
